# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： tmp_728.py
# @Time：2023/7/28 14:52
# @Author：liaogan

from selenium import webdriver
from time import sleep
import ddddocr  # 简单的OCR库 要自行安装 ddddocr
from selenium.webdriver.common.by import By


driver = webdriver.Chrome()
driver.get('http://testview.booseng.com/hwyd/#/login')
# driver.get('https://jky.huawangglobal.com/#/login')
driver.implicitly_wait(10)
driver.maximize_window()

num = 5
for one in range(num):

    ele_code = driver.find_element(By.XPATH, '//div/img')
    with open('code.png','wb') as f:
        f.write(ele_code.screenshot_as_png)  #写入到一个图片

    ocr = ddddocr.DdddOcr(show_ad=False)  # 初始化一个ocr对象
    code = ocr.classification(ele_code.screenshot_as_png)  # 当前验证码的字符
    print(code[0], code[1], code[2])
    if code[1] == "+":
        res = int(code[0]) + int(code[2])
    elif code[1] == "-":
        res = int(code[0]) + int(code[2])
    elif code[1] == 'x':
        res = int(code[0]) * int(code[2])
    elif code[1] == '/':
        res = int(code[0]) / int(code[2])
    else:
        res = '123456'

    user = driver.find_element(By.XPATH, '//*[@placeholder="账号"]')
    password = driver.find_element(By.XPATH, '//*[@placeholder="密码"]')
    yzm = driver.find_element(By.XPATH, '//*[@placeholder="验证码"]')

    user.clear()
    password.clear()
    yzm.clear()

    user.send_keys('admin')
    password.send_keys('123456')
    yzm.send_keys(res)
    driver.find_element(By.CSS_SELECTOR, '[class="el-button el-button--primary el-button--medium"]').click()
    sleep(2)
    success_url = driver.current_url
    print(('url', success_url))
    if "#/dashboard" in success_url:
        print("登录成功")
        token=driver.get_cookies()[0]['value'].replace('%20', ' ')
        print(token)
        driver.close()
        break

# user=driver.find_element(By.CSS_SELECTOR,'[class="el-input__inner"][placeholder="账号"]').send_keys('admin')
# pawword=driver.find_element(By.CSS_SELECTOR,'input[placeholder="密码"][class="el-input__inner"]').send_keys('123456')
# yzm=driver.find_element(By.CSS_SELECTOR,'[placeholder="验证码"]').send_keys(123456)
# aa=driver.find_element(By.CSS_SELECTOR,'[class="el-checkbox__inner"]').click()
# dl=driver.find_element(By.CSS_SELECTOR,'[class="el-button el-button--primary el-button--medium"]').click()
